﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.Model.DTO;
using MyBlog.WebApi.Utility._MD5;
using MyBlog.WebApi.Utility.ApiResult;

namespace MyBlog.Api.Controllers
{
    /*
     作者（用户）控制器
     */
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class WriterInfoController : ControllerBase
    {

        #region 固定注入

        private readonly IWriterInfoService _iWriterInfoService;
        public WriterInfoController(IWriterInfoService iWriterInfoService)
        {
            this._iWriterInfoService = iWriterInfoService;
        }

        #endregion

        /// <summary>
        /// 查找作者信息[匿名使用]
        /// </summary>
        /// <param name="iMapper"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("FindWriter")]
        public async Task<ApiResult> FindWriter([FromServices] IMapper iMapper, int id)
        {
            // 从服务中 获取Mappper 映射 [FromServices] IMapper iMapper
            var writer = await _iWriterInfoService.FindAsync(id);
            var writerDTO = iMapper.Map<WriterDTO>(writer);
            return ApiResultHelper.Success(writerDTO);
        }

        /// <summary>
        ///  创建作者账号[鉴权使用]
        /// </summary>
        /// <param name="name"></param>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ApiResult> Create(string name, string username, string userpwd)
        {
            //数据校验
            WriterInfo writer = new WriterInfo
            {
                Name = name,
                //加密密码
                UserPwd = MD5Helper.MD5Encrypt32(userpwd),
                UserName = username
            };
            //判断数据库中是否已经存在账号跟要添加的账号相同的数据
            var oldWriter = await _iWriterInfoService.FindAsync(c => c.UserName == username);
            if (oldWriter != null) return ApiResultHelper.Error("账号已经存在");

            bool b = await _iWriterInfoService.CreateAsync(writer);
            if (!b) return ApiResultHelper.Error("添加失败");
            return ApiResultHelper.Success(writer);
        }

        /// <summary>
        /// 删除账号[鉴权使用]
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            bool b = await _iWriterInfoService.DeleteAsync(id);
            if (!b) return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }

        /// <summary>
        /// 修改账号[鉴权使用]
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ApiResult> Edit(string name)
        {
            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            var writer = await _iWriterInfoService.FindAsync(id);
            writer.Name = name;
            bool b = await _iWriterInfoService.EditAsync(writer);
            if (!b) return ApiResultHelper.Error("修改失败");
            return ApiResultHelper.Success("修改成功");
        }

    }
}
